package nimy.info.android.dinnertime.adapter;

import info.nimy.google.recipesearch.html.RecipeItem;

import java.util.List;

import nimy.info.android.dinnertime.HomeActivity;
import nimy.info.android.dinnertime.R;
import nimy.info.android.dinnertime.page.NoDuplicatedArrayList;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

public class RecipeItemAdapter extends BaseAdapter {

	private static final String BASE64_PREFIX = "base64,";

	private final List<RecipeItem> items;
	private final Context context;

	public RecipeItemAdapter(Context context) {
		super();
		this.items = new NoDuplicatedArrayList<RecipeItem>(20);
		this.context = context;
	}

	@Override
	public int getCount() {
		return items.size();
	}

	@Override
	public Object getItem(int position) {
		return items.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater mInflater = LayoutInflater.from(context);

		View itemView = mInflater.inflate(R.layout.list_item_recipe_summary,
				null);
		RecipeItem recipe = (RecipeItem) getItem(position);

		// title
		TextView nameView = (TextView) itemView.findViewById(R.id.recipe_title);
		nameView.setText(recipe.getName());

		// summary
		TextView detailView = (TextView) itemView
				.findViewById(R.id.recipe_summary);
		detailView.setText(recipe.getDetail());

		// cite
		String cite = recipe.getCite();
		TextView citeView = (TextView) itemView
				.findViewById(R.id.recipe_summay_cite);
		if (cite == null || cite.trim().equals("")) {
			citeView.setVisibility(View.GONE);
		} else {
			citeView.setText(cite);
		}

		// rating bar
		RatingBar ratingBar = (RatingBar) itemView
				.findViewById(R.id.recipe_summary_ratingbar);
		ratingBar.setRating((float) recipe.getRate());

		ImageView iconView = (ImageView) itemView
				.findViewById(R.id.recipe_icon);
		iconView.setFocusable(false);
		iconView.setFocusableInTouchMode(false);
		if (iconView != null) {
			String icon = recipe.getIcon();
			if (icon != null && !icon.trim().equals("")) {
				String base64Icon = icon.substring(icon.indexOf(BASE64_PREFIX)
						+ BASE64_PREFIX.length());
				byte[] decodedString = Base64
						.decode(base64Icon, Base64.DEFAULT);
				Bitmap bitmap = BitmapFactory.decodeByteArray(decodedString, 0,
						decodedString.length);
				iconView.setImageBitmap(bitmap);
			}
		}
		return itemView;

	}

	public void addRecipe(RecipeItem item) {
		if (items.add(item)) {
			notifyDataSetChanged();
		}
	}

	public void clear() {
		if (!items.isEmpty()) {
			items.clear();
			notifyDataSetChanged();
		}
	}

	/**
	 * Add new recipes items to memory
	 * 
	 * @param result
	 * @return
	 */
	public void addRecipes(final List<RecipeItem> result) {
		if (HomeActivity.DEBUG) {
			Log.d("RecipeItemAdapter", "Update recipe item list view");
		}
		if (items.addAll(result)) {
			notifyDataSetChanged();
		}
	}
}
